<?php
/**
 *
 * tree.php
 * @author Artem Azarov <bel-azar@ya.ru>
 * Date: 5/27/12
 * Time: 10:34 AM
 *
 * @TODO this is ugly ugly uglyyyyyyyy
 */

$this->breadcrumbs = array(
    $cat->name => array('cat/view', 'id'=>$cat->cat_id),
    Yii::t('menu', 'Genealogy'),
);
?>


<table cellspacing="0" class="genealogy-tree">
    <tbody>
    <tr>
        <td class="td_b" rowspan="8" data-name="father">
            <span><?php echo $collection->father->name?></span><p></p>
            <p><?php echo $collection->father->colour?></p>
            <?php if ($collection->father->avatar):?>
                <p><img alt="" height="100" src="<?php echo $collection->father->avatar?>" width="150" /></p>
            <?php endif ?>
        </td>
        <td class="td_b" rowspan="4" data-name="fatherFather">
            <span><?php echo $collection->fatherFather->name?></span><p></p>
            <p><?php echo $collection->fatherFather->colour?></p>
            <?php if ($collection->fatherFather->avatar):?>
                <p><img alt="" height="100" src="<?php echo $collection->fatherFather->avatar?>" width="150" /></p>
            <?php endif ?>
        </td>
        <td class="td_b" rowspan="2" data-name="fatherFatherFather">
            <span><?php echo $collection->fatherFatherFather->name?></span><p></p>
            <p><?php echo $collection->fatherFatherFather->colour?></p>
            <?php if ($collection->fatherFatherFather->avatar):?>
                <p><img alt="" height="100" src="<?php echo $collection->fatherFatherFather->avatar?>" width="150" /></p>
            <?php endif ?>
        </td>
        <td data-name="fatherFatherFatherFather">
            <span><?php echo $collection->fatherFatherFatherFather->name?></span><p></p>
            <p><?php echo $collection->fatherFatherFatherFather->colour?></p>
            <?php if ($collection->fatherFatherFatherFather->avatar):?>
                <p><img alt="" height="100" src="<?php echo $collection->fatherFatherFatherFather->avatar?>" width="150" /></p>
            <?php endif ?>
        </td>
    </tr>
    <tr>
        <td class="td_b" data-name="fatherFatherFatherMother">
            <span><?php echo $collection->fatherFatherFatherMother->name?></span><p></p>
            <p><?php echo $collection->fatherFatherFatherMother->colour?></p>
            <?php if ($collection->fatherFatherFatherMother->avatar):?>
                <p><img alt="" height="100" src="<?php echo $collection->fatherFatherFatherMother->avatar?>" width="150" /></p>
            <?php endif ?>
    </tr>
    <tr>
        <td class="td_b" rowspan="2" data-name="fatherFatherMother">
            <span><?php echo $collection->fatherFatherMother->name?></span><p></p>
            <p><?php echo $collection->fatherFatherMother->colour?></p>
            <?php if ($collection->fatherFatherMother->avatar):?>
                <p><img alt="" height="100" src="<?php echo $collection->fatherFatherMother->avatar?>" width="150" /></p>
            <?php endif ?>
        </td>
        <td data-name="fatherFatherMotherFather">
            <span><?php echo $collection->fatherFatherMotherFather->name?></span><p></p>
            <p><?php echo $collection->fatherFatherMotherFather->colour?></p>
            <?php if ($collection->fatherFatherMotherFather->avatar):?>
                <p><img alt="" height="100" src="<?php echo $collection->fatherFatherMotherFather->avatar?>" width="150" /></p>
            <?php endif ?>
        </td>
    </tr>
    <tr>
        <td class="td_b" data-name="fatherFatherMotherMother">
            <span><?php echo $collection->fatherFatherMotherMother->name?></span><p></p>
            <p><?php echo $collection->fatherFatherMotherMother->colour?></p>
            <?php if ($collection->fatherFatherMotherMother->avatar):?>
                <p><img alt="" height="100" src="<?php echo $collection->fatherFatherMotherMother->avatar?>" width="150" /></p>
            <?php endif ?>
        </td>
    </tr>
    <tr>
        <td class="td_b" rowspan="4" data-name="fatherMother">
            <span><?php echo $collection->fatherMother->name?></span><p></p>
            <p><?php echo $collection->fatherMother->colour?></p>
            <?php if ($collection->fatherMother->avatar):?>
                <p><img alt="" height="100" src="<?php echo $collection->fatherMother->avatar?>" width="150" /></p>
            <?php endif ?>
        </td>
        <td class="td_b" rowspan="2" data-name="fatherMotherFather">
            <span><?php echo $collection->fatherMotherFather->name?></span><p></p>
            <p><?php echo $collection->fatherMotherFather->colour?></p>
            <?php if ($collection->fatherMotherFather->avatar):?>
                <p><img alt="" height="100" src="<?php echo $collection->fatherMotherFather->avatar?>" width="150" /></p>
            <?php endif ?>
        </td>
        <td data-name="fatherMotherFatherFather">
            <span><?php echo $collection->fatherMotherFatherFather->name?></span><p></p>
            <p><?php echo $collection->fatherMotherFatherFather->colour?></p>
            <?php if ($collection->fatherMotherFatherFather->avatar):?>
                <p><img alt="" height="100" src="<?php echo $collection->fatherMotherFatherFather->avatar?>" width="150" /></p>
            <?php endif ?>
        </td>
    </tr>
    <tr>
        <td class="td_b" data-name="fatherMotherFatherMother">
            <span><?php echo $collection->fatherMotherFatherMother->name?></span><p></p>
            <p><?php echo $collection->fatherMotherFatherMother->colour?></p>
            <?php if ($collection->fatherMotherFatherMother->avatar):?>
                <p><img alt="" height="100" src="<?php echo $collection->fatherMotherFatherMother->avatar?>" width="150" /></p>
            <?php endif ?>
        </td>
    </tr>
    <tr>
        <td class="td_b" rowspan="2" data-name="fatherMotherMother">
            <span><?php echo $collection->fatherMotherMother->name?></span><p></p>
            <p><?php echo $collection->fatherMotherMother->colour?></p>
            <?php if ($collection->fatherMotherMother->avatar):?>
              <p><img alt="" height="100" src="<?php echo $collection->fatherMotherMother->avatar?>" width="150" /></p>
            <?php endif ?>
        </td>
        <td data-name="fatherMotherMotherFather">
            <span><?php echo $collection->fatherMotherMotherFather->name?></span><p></p>
            <p><?php echo $collection->fatherMotherMotherFather->colour?></p>
            <?php if ($collection->fatherMotherMotherFather->avatar):?>
                <p><img alt="" height="100" src="<?php echo $collection->fatherMotherMotherFather->avatar?>" width="150" /></p>
            <?php endif ?>
        </td>
    </tr>
    <tr>
        <td class="td_b" data-name="fatherMotherMotherMother">
            <span><?php echo $collection->fatherMotherMotherMother->name?></span><p></p>
            <p><?php echo $collection->fatherMotherMotherMother->colour?></p>
            <?php if ($collection->fatherMotherMotherMother->avatar):?>
                <p><img alt="" height="100" src="<?php echo $collection->fatherMotherMotherMother->avatar?>" width="150" /></p>
            <?php endif ?>
        </td>
    </tr>
    <tr>
        <td class="td_b" rowspan="8" data-name="mother">
            <span><?php echo $collection->mother->name?></span><p></p>
            <p><?php echo $collection->mother->colour?></p>
            <?php if ($collection->mother->avatar):?>
            <p><img alt="" height="100" src="<?php echo $collection->mother->avatar?>" width="150" /></p>
            <?php endif ?>
        </td>
        <td class="td_b" rowspan="4" data-name="motherFather">
            <span><?php echo $collection->motherFather->name?></span><p></p>
            <p><?php echo $collection->motherFather->colour?></p>
            <?php if ($collection->motherFather->avatar):?>
            <p><img alt="" height="100" src="<?php echo $collection->motherFather->avatar?>" width="150" /></p>
            <?php endif ?>
        </td>
        <td class="td_b" rowspan="2" data-name="motherFatherFather">
            <span><?php echo $collection->motherFatherFather->name?></span><p></p>
            <p><?php echo $collection->motherFatherFather->colour?></p>
            <?php if ($collection->motherFatherFather->avatar):?>
            <p><img alt="" height="100" src="<?php echo $collection->motherFatherFather->avatar?>" width="150" /></p>
            <?php endif ?>
        </td>
        <td data-name="motherFatherFatherFather">
            <span><?php echo $collection->motherFatherFatherFather->name?></span><p></p>
            <p><?php echo $collection->motherFatherFatherFather->colour?></p>
            <?php if ($collection->motherFatherFatherFather->avatar):?>
            <p><img alt="" height="100" src="<?php echo $collection->motherFatherFatherFather->avatar?>" width="150" /></p>
            <?php endif ?>
        </td>
    </tr>
    <tr>
        <td class="td_b" data-name="motherFatherFatherMother">
            <span><?php echo $collection->motherFatherFatherMother->name?></span><p></p>
            <p><?php echo $collection->motherFatherFatherMother->colour?></p>
            <?php if ($collection->motherFatherFatherMother->avatar):?>
            <p><img alt="" height="100" src="<?php echo $collection->motherFatherFatherMother->avatar?>" width="150" /></p>
            <?php endif ?>
    </tr>
    <tr>
        <td class="td_b" rowspan="2" data-name="motherFatherMother">
            <span><?php echo $collection->motherFatherMother->name?></span><p></p>
            <p><?php echo $collection->motherFatherMother->colour?></p>
            <?php if ($collection->motherFatherMother->avatar):?>
            <p><img alt="" height="100" src="<?php echo $collection->mother->avatar?>" width="150" /></p>
            <?php endif ?>
        </td>
        <td data-name="motherFatherMotherFather">
            <span><?php echo $collection->motherFatherMotherFather->name?></span><p></p>
            <p><?php echo $collection->motherFatherMotherFather->colour?></p>
            <?php if ($collection->motherFatherMotherFather->avatar):?>
            <p><img alt="" height="100" src="<?php echo $collection->motherFatherMotherFather->avatar?>" width="150" /></p>
            <?php endif ?>
        </td>
    </tr>
    <tr>
        <td class="td_b" data-name="motherFatherMotherMother">
            <span><?php echo $collection->motherFatherMotherMother->name?></span><p></p>
            <p><?php echo $collection->motherFatherMotherMother->colour?></p>
            <?php if ($collection->motherFatherMotherMother->avatar):?>
            <p><img alt="" height="100" src="<?php echo $collection->motherFatherMotherMother->avatar?>" width="150" /></p>
            <?php endif ?>
        </td>
    </tr>
    <tr>
        <td class="td_b" rowspan="4" data-name="motherMother">
            <span><?php echo $collection->motherMother->name?></span><p></p>
            <p><?php echo $collection->motherMother->colour?></p>
            <?php if ($collection->motherMother->avatar):?>
            <p><img alt="" height="100" src="<?php echo $collection->motherMother->avatar?>" width="150" /></p>
            <?php endif ?>
        </td>
        <td class="td_b" rowspan="2" data-name="motherMotherFather">
            <span><?php echo $collection->motherMotherFather->name?></span><p></p>
            <p><?php echo $collection->motherMotherFather->colour?></p>
            <?php if ($collection->motherMotherFather->avatar):?>
            <p><img alt="" height="100" src="<?php echo $collection->motherMotherFather->avatar?>" width="150" /></p>
            <?php endif ?>
        </td>
        <td data-name="motherMotherFatherFather">
            <span><?php echo $collection->motherMotherFatherFather->name?></span><p></p>
            <p><?php echo $collection->motherMotherFatherFather->colour?></p>
            <?php if ($collection->motherMotherFatherFather->avatar):?>
            <p><img alt="" height="100" src="<?php echo $collection->motherMotherFatherFather->avatar?>" width="150" /></p>
            <?php endif ?>
        </td>
    </tr>
    <tr>
        <td class="td_b" data-name="motherMotherFatherMother">
            <span><?php echo $collection->motherMotherFatherMother->name?></span><p></p>
            <p><?php echo $collection->motherMotherFatherMother->colour?></p>
            <?php if ($collection->motherMotherFatherMother->avatar):?>
            <p><img alt="" height="100" src="<?php echo $collection->motherMotherFatherMother->avatar?>" width="150" /></p>
            <?php endif ?>
        </td>
    </tr>
    <tr>
        <td class="td_b" rowspan="2" data-name="motherMotherMother">
            <span><?php echo $collection->motherMotherMother->name?></span><p></p>
            <p><?php echo $collection->motherMotherMother->colour?></p>
            <?php if ($collection->motherMotherMother->avatar):?>
            <p><img alt="" height="100" src="<?php echo $collection->motherMotherMother->avatar?>" width="150" /></p>
            <?php endif ?>
        </td>
        <td data-name="motherMotherMotherFather">
            <span><?php echo $collection->motherMotherMotherFather->name?></span><p></p>
            <p><?php echo $collection->motherMotherMotherFather->colour?></p>
            <?php if ($collection->motherMotherMotherFather->avatar):?>
            <p><img alt="" height="100" src="<?php echo $collection->motherMotherMotherFather->avatar?>" width="150" /></p>
            <?php endif ?>
        </td>
    </tr>
    <tr>
        <td class="td_b" data-name="motherMotherMotherMother">
            <span><?php echo $collection->motherMotherMotherMother->name?></span><p></p>
            <p><?php echo $collection->motherMotherMotherMother->colour?></p>
            <?php if ($collection->motherMotherMotherMother->avatar):?>
            <p><img alt="" height="100" src="<?php echo $collection->motherMotherMotherMother->avatar?>" width="150" /></p>
            <?php endif ?>
        </td>
    </tr>
    </tbody>
</table>

<!--<table class="genealogy-tree">
    <?php  //$this->renderPartial('_father', array('tree'=>$tree, 'level'=>4)) ?>
    <?php  //$this->renderPartial('_mother', array('tree'=>$tree, 'level'=>4)) ?>
</table>
-->

<?php
    //overlay
    $this->beginWidget('zii.widgets.jui.CJuiDialog', array(
        'id'=>'genealogy',
        // additional javascript options for the dialog plugin
        'options'=>array(
            'title'=>'Genealogy',
            'autoOpen'=>false,
            'width'=>'60%',
        ),
    ));

    echo CHtml::beginForm(null,'post', array('id'=>'genealogy_update'));
    echo CHtml::hiddenField('cat_id', Yii::app()->getRequest()->getParam('id', false));
    echo CHtml::hiddenField('code');
    echo CHtml::hiddenField('avatar');
    echo CHtml::label('Name','name');
    echo CHtml::textField('name');
    echo CHtml::label('Colour','colour');
    echo CHtml::textField('colour');
    echo CHtml::image('','', array('name'=>'avatar_url', 'id'=>'avatar_url'));
        $this->widget('application.modules.imgManager.extensions.plupload.PluploadWidget', array(
            'config' => array(
                'runtimes' => 'gears,flash,silverlight,browserplus,html5',
                'multipart_params' => array("YII_CSRF_TOKEN" => Yii::app()->request->csrfToken),
                'url' => $this->createUrl('genealogy/imageUpload/'),
                'chunk_size' => '1mb',
                'unique_names' => true,
                'max_file_number' => 1,
                'filters' => array(
                    array('title' => Yii::t('app', 'Images files'), 'extensions' => 'jpg,jpeg,gif,png'),
                ),
                'language' => Yii::app()->language,
                'autostart' => false,
                'jquery_ui' => false,
                'reset_after_upload' => true,
            ),
            'callbacks' => array(
                'UploadComplete' => 'function(uploader,files){
                              uploader.refresh();
                 }',
                "FileUploaded" => "function(uploader,file,response){
                    var resp = $.parseJSON(response.response);
                    $('#genealogy_update').find('#avatar_url').attr('src',resp.upload_path+'/'+resp.file_id+resp.file_ext);
                    $('#genealogy_update').find('#avatar').val($('#genealogy_update').find('#avatar_url').attr('src'));
                    uploader.refresh();
                 }"
            ),
            'id' => 'uploader'
        ));
    echo CHtml::submitButton('save');
    echo CHtml::endForm();

    $this->endWidget('zii.widgets.jui.CJuiDialog');
?>
<script type="text/javascript">
    $.ready(
        $('.genealogy-tree td').click(function(){
            $("#genealogy").dialog("open");
            //init
            $("#genealogy_update")[0].reset();
            $("#genealogy_update").find('#avatar_url').attr('src','#');

            $("#genealogy_update").find('#code').val($(this).data('name'));
            $("#genealogy_update").find('#name').val($(this).find('span').html());
            $("#genealogy_update").find('#colour').val($($(this).find('p')[1]).html());
            $("#genealogy_update").find('#avatar_url').attr('src',$(this).find('img').attr('src'));
            $('#genealogy_update').find('#avatar').val($(this).find('img').attr('src'));
        }),
        $('#genealogy_update').submit(function(e){
            e.preventDefault();
            $.post('/genealogy/saveEntity', $("#genealogy_update").serialize(), function(){
                var tableCell = $('.genealogy-tree [data-name="'+$("#genealogy_update").find('#code').val()+'"]');
                tableCell.find('span').html($("#genealogy_update").find('#name').val());
                $(tableCell.find('p')[1]).html($("#genealogy_update").find('#colour').val());
                tableCell.find('img').attr('src',$("#genealogy_update").find('#avatar_url').attr('src'));
            });

            $("#genealogy").dialog("close")
        })
    )
</script>